package com.sgb.goods.viewmodel.publish;

import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ColorUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.goods.R;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.databinding.GoodsActivityGoodsPublishBinding;
import com.sgb.goods.entity.AddLayoutEntity;
import com.sgb.goods.entity.AlbumEntity;
import com.sgb.goods.entity.OtherInfo;
import com.sgb.goods.entity.VideoChangeEntity;
import com.sgb.goods.entity.VideoEntity;
import com.sgb.goods.utils.base.GoodsSdk;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.publish.ImageAdapter;
import com.sgb.goods.view.adapter.publish.VideoAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.customview.TextAreaEditView;
import com.swgk.core.util.DensityUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态控件辅助类
 * 用于网络动态组件
 * @author cyj
 */
public class DynamicComponentsViewModel extends BaseViewModel {

    public static final int IMAGE_ADAPTER_LIST = 110119;
    public static final int VIDEO_ADAPTER_LIST = 119110;
    public MutableLiveData<List<AddLayoutEntity<List<AddLayoutEntity>>>> addLayoutLD = new MutableLiveData<>();
    /** 用于显示视频选择的弹窗 */
    public MutableLiveData<Integer> showVideoListSelectPopLD = new MutableLiveData<>();
    public Map<Integer, ImageAdapter> mPImgAdapter = new HashMap();
    public int mImageAdapterPosition;
    public int mVideoAdapterPosition;
    public Map<Integer, Integer> mPCurrentImgNum = new HashMap();
    public Map<Integer, List<String>> mPUpUrl = new HashMap();
    public Map<Integer, Integer> mPVideoIndex = new HashMap();

    public Map<Integer, VideoAdapter> mPVideoAdapter = new HashMap();
    public Map<Integer, List<File>> mPVideoFile = new HashMap();

    //其他数据
    public List<AddLayoutEntity<List<AddLayoutEntity>>> addLayoutEntityList;
    public Map<Integer, ItemInputTextView> mPItv = new HashMap();
    public Map<Integer, TextAreaEditView> mPTae = new HashMap();
    
    /** 获取商品其他信息 */
    public void getAddLayout(String cid) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().getAddLayout(cid).enqueue(new GoodsCallBack<BaseEntity<List<AddLayoutEntity<List<AddLayoutEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddLayoutEntity<List<AddLayoutEntity>>>> data) {
                addLayoutLD.setValue(data.getData());
            }
        });
    }

    /** 编辑时候获取商品其他信息 */
    public void getGoodsOtherInfo(String id) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().getGoodsOtherInfo(id).enqueue(new GoodsCallBack<BaseEntity<List<AddLayoutEntity<List<AddLayoutEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddLayoutEntity<List<AddLayoutEntity>>>> data) {
                addLayoutLD.setValue(data.getData());
            }
        });
    }

    /** 编辑时候获取商砼其他信息 */
    public void getStOtherInfo(String id) {
        showLoading();
        GoodsNet.getInstance().getGoodsPublishApi().getStOtherInfo(id).enqueue(new GoodsCallBack<BaseEntity<List<AddLayoutEntity<List<AddLayoutEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddLayoutEntity<List<AddLayoutEntity>>>> data) {
                addLayoutLD.setValue(data.getData());
            }
        });
    }

    /** 获取其他数据。主要是动态控件的数据。 */
    public List<OtherInfo> getOtherData() {
        List<OtherInfo> otherInfoList = new ArrayList<>();
        if (addLayoutEntityList != null) {
            for (int i = 0; i < addLayoutEntityList.size(); i++) {
                for (int j = 0; j < addLayoutEntityList.get(i).subColumns.size(); j++) {
                    int id = addLayoutEntityList.get(i).subColumns.get(j).id;
                    if ("input".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        OtherInfo otherInfo = new OtherInfo();
                        otherInfo.id = addLayoutEntityList.get(i).subColumns.get(j).id;
                        otherInfo.titleColumnVal = mPItv.get(id).getContent();
                        otherInfoList.add(otherInfo);
                    } else if ("textarea".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        OtherInfo otherInfo = new OtherInfo();
                        otherInfo.id = addLayoutEntityList.get(i).subColumns.get(j).id;
                        otherInfo.titleColumnVal = mPTae.get(id).getEditContent();
                        otherInfoList.add(otherInfo);
                    } else if ("pic".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        OtherInfo otherInfo = new OtherInfo();
                        otherInfo.id = addLayoutEntityList.get(i).subColumns.get(j).id;
                        if (mPUpUrl.get(id).size() != 0) {
                            otherInfo.titleColumnVal = new Gson().toJson(mPUpUrl.get(id));
                        } else if (mPImgAdapter.get(id).getDataSize() != 0) {
                            otherInfo.titleColumnVal = new Gson().toJson(mPImgAdapter.get(id).getData());
                        } else {
                            otherInfo.titleColumnVal = "";
                        }
                        otherInfoList.add(otherInfo);
                    } else if ("video".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        OtherInfo otherInfo = new OtherInfo();
                        otherInfo.id = addLayoutEntityList.get(i).subColumns.get(j).id;
                        if (mPVideoAdapter.get(id).getData().size() != 0) {
                            otherInfo.titleColumnVal = new Gson().toJson(mPVideoAdapter.get(id).getData());
                        } else {
                            otherInfo.titleColumnVal = "";
                        }
                        otherInfoList.add(otherInfo);
                    }
                }
            }
        }
        return otherInfoList;
    }

    /** 动态创建视图布局 */
    public void addMoveLayout(GoodsActivityGoodsPublishBinding mDataBinding, List<AddLayoutEntity<List<AddLayoutEntity>>> addLayoutEntityList) {
        this.addLayoutEntityList = addLayoutEntityList;
        List<ItemInputTextView> itemInputTextViews = new ArrayList<>();
        List<LinearLayout> linearLayouts = new ArrayList<>();
        mDataBinding.llOther.removeAllViewsInLayout();
        mDataBinding.llOther.addView(mDataBinding.tvOther);
        if (this.addLayoutEntityList != null && this.addLayoutEntityList.size() != 0) {
            mDataBinding.tvOther.setVisibility(View.VISIBLE);
            for (int i = 0; i < addLayoutEntityList.size(); i++) {
                ItemInputTextView pitemInputTextView = new ItemInputTextView(GoodsSdk.getInstance().getTopActivity());
                //子布局
                LinearLayout pLinearLayout = new LinearLayout(application);
                pLinearLayout.setVisibility(View.GONE);
                pLinearLayout.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA));
                pLinearLayout.setOrientation(LinearLayout.VERTICAL);
                mDataBinding.llOther.addView(pitemInputTextView);
                mDataBinding.llOther.addView(pLinearLayout);
                //获取TextView对应的LayoutParams
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) pitemInputTextView.getLayoutParams();
                pitemInputTextView.setBackgroundColor(getResources().getColor(R.color.white));
                layoutParams.height = DensityUtils.dip2px(application, 50);
                LinearLayout.LayoutParams pLinearLayoutLayoutParams = (LinearLayout.LayoutParams) pLinearLayout.getLayoutParams();
                pLinearLayoutLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);
                pLinearLayoutLayoutParams.rightMargin = DensityUtils.dip2px(application, 10);
                pitemInputTextView.setLabelName(addLayoutEntityList.get(i).name);
                pitemInputTextView.setEtEnabled(false);
                pitemInputTextView.setDefRightImg();
                pitemInputTextView.setDefRightImgMargin(10);
                itemInputTextViews.add(pitemInputTextView);
                linearLayouts.add(pLinearLayout);
                int finalI1 = i;
                itemInputTextViews.get(i).setOnWholeItemClickListener(view -> {
                    if (linearLayouts.get(finalI1).getVisibility() == View.GONE) {
                        //显示布局
                        linearLayouts.get(finalI1).setVisibility(View.VISIBLE);
                        //隐藏下划线
                        itemInputTextViews.get(finalI1).setDeliverLineVG(false);
                        //滑到最底部
                        mDataBinding.nS.smoothScrollTo(0, 9999);
                        itemInputTextViews.get(finalI1).setRightImg(true);
                    } else {
                        //隐藏布局
                        linearLayouts.get(finalI1).setVisibility(View.GONE);
                        //显示下划线
                        itemInputTextViews.get(finalI1).setDeliverLineVG(true);
                        itemInputTextViews.get(finalI1).setRightImg(false);
                    }
                });
                //最后一个隐藏下划线
                if (i == addLayoutEntityList.size() - 1)
                    pitemInputTextView.setDeliverLineVG(false);
                for (int j = 0; j < addLayoutEntityList.get(i).subColumns.size(); j++) {
                    int childPosition = j;
                    if ("input".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        //画输入框
                        ItemInputTextView itemInputTextView = new ItemInputTextView(GoodsSdk.getInstance().getTopActivity());
                        pLinearLayout.addView(itemInputTextView);
                        //获取TextView对应的LayoutParams
                        LinearLayout.LayoutParams itemInputTextViewLayoutParams = (LinearLayout.LayoutParams) itemInputTextView.getLayoutParams();
                        //设置输入框背景颜色
                        itemInputTextView.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA));
                        //设置输入框高度
                        itemInputTextViewLayoutParams.height = DensityUtils.dip2px(application, 50);
                        //设置输入框名称
                        itemInputTextView.setLabelName(addLayoutEntityList.get(i).subColumns.get(j).name);
                        //设置输入框hint
                        itemInputTextView.setHint("请输入" + addLayoutEntityList.get(i).subColumns.get(j).name);
                        //设置输入框标题边距
                        itemInputTextView.setLabelMargin(10);
                        //字数限制
                        itemInputTextView.setEditLength(50);
                        if (addLayoutEntityList.get(i).subColumns.size() - 1 == j) {
                            itemInputTextView.setDeliverLineVG(false);
                        }
                        //设置输入框底部线边距
                        itemInputTextView.setLinMargin(5, 0);
                        //设置输入框可输入
                        itemInputTextView.setEnable(true);
                        //设置输入框可输入
                        itemInputTextView.setArrowVisible(false);
                        //数据回显
                        //判断文本框内容是否为空
                        if (!TextUtils.isEmpty(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal)) {
                            itemInputTextView.setContent(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal);
                        }
                        mPItv.put(addLayoutEntityList.get(i).subColumns.get(j).id, itemInputTextView);
                    } else if ("textarea".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        //创建文本框
                        TextAreaEditView textAreaEditView = new TextAreaEditView(application);
                        //设置文本框的标题名称
                        textAreaEditView.setTitleStr(addLayoutEntityList.get(i).subColumns.get(j).name);
                        //设置Hint提示语
                        textAreaEditView.setEtHint("请输入" + addLayoutEntityList.get(i).subColumns.get(j).name);
                        //设置最大输入字数
                        textAreaEditView.setMaxText(300);
                        //加入父布局
                        pLinearLayout.addView(textAreaEditView);
                        if (addLayoutEntityList.get(i).subColumns.size() - 1 == j)
                            textAreaEditView.setTvLine(View.GONE);
                        if (!TextUtils.isEmpty(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal)) {
                            textAreaEditView.setEditContent(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal);
                        }
                        mPTae.put(addLayoutEntityList.get(i).subColumns.get(j).id, textAreaEditView);
                    } else if ("pic".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        //图片控件
                        //外层布局
                        LinearLayout linearLayout = new LinearLayout(application);
                        //加入父布局
                        pLinearLayout.addView(linearLayout);
                        //设置本层布局的背景颜色
                        linearLayout.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA));
                        //设置垂直布局
                        linearLayout.setOrientation(LinearLayout.VERTICAL);
                        //创建一个标题控件标题
                        TextView mTitleTextView = new TextView(application);
                        //把标题控件加入本层布局
                        linearLayout.addView(mTitleTextView);
                        //得到标题控件的 LayoutParams
                        LinearLayout.LayoutParams textViewLayoutParams = (LinearLayout.LayoutParams) mTitleTextView.getLayoutParams();
                        //设置标题边距
                        textViewLayoutParams.bottomMargin = DensityUtils.dip2px(application, 10);
                        textViewLayoutParams.topMargin = DensityUtils.dip2px(application, 10);
                        textViewLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);
                        //设置标题文字
                        mTitleTextView.setText(addLayoutEntityList.get(i).subColumns.get(j).name);
                        //设置标题颜色
                        mTitleTextView.setTextColor(getResources().getColor(R.color.color_333333));
                        //图片recyclerView
                        RecyclerView recyclerView = new RecyclerView(application);
                        //recyclerView加入本层布局
                        linearLayout.addView(recyclerView);
                        if (j != addLayoutEntityList.get(i).subColumns.size() - 1) {
                            //分割线
                            TextView line = new TextView(application);
                            //加入当前父布局
                            linearLayout.addView(line);
                            line.setBackgroundColor(getResources().getColor(R.color.color_EDEDED));
                            LinearLayout.LayoutParams lineLayoutParams = (LinearLayout.LayoutParams) line.getLayoutParams();
                            lineLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;//充满
                            lineLayoutParams.height = 1;
                            lineLayoutParams.rightMargin = DensityUtils.dip2px(application, 5);
                            lineLayoutParams.topMargin = DensityUtils.dip2px(application, 10);
                            lineLayoutParams.leftMargin = DensityUtils.dip2px(application, 5);
                        }
                        //拿到recyclerView的LayoutParams
                        LinearLayout.LayoutParams recyclerViewLayoutParams = (LinearLayout.LayoutParams) recyclerView.getLayoutParams();
                        //设置边距
                        recyclerViewLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);
                        //创建图片选择的adapter
                        ImageAdapter imageAdapter = new ImageAdapter(5);
                        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
                        recyclerView.setLayoutManager(new
                                GridLayoutManager(application, 4));
                        recyclerView.setAdapter(imageAdapter);

                        mPImgAdapter.put(addLayoutEntityList.get(i).subColumns.get(j).id, imageAdapter);
                        //数据回显
                        if (!TextUtils.isEmpty(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal) && !"[]".equals(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal)) {
                            List<String> urls = new Gson().fromJson(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal, new TypeToken<List<String>>() {
                            }.getType());
                            List<AlbumEntity> albumEntityList = new ArrayList<>(urls.size());
                            for(String url : urls) {
                                AlbumEntity entity = new AlbumEntity();
                                entity.imageUrl = url;
                                albumEntityList.add(entity);
                            }
                            mPImgAdapter.get(addLayoutEntityList.get(i).subColumns.get(j).id).addData(albumEntityList);
                        }
                        mPUpUrl.put(addLayoutEntityList.get(i).subColumns.get(j).id, new ArrayList<>());
                        mPCurrentImgNum.put(addLayoutEntityList.get(i).subColumns.get(j).id, 0);
                        mPImgAdapter.get(addLayoutEntityList.get(i).subColumns.get(j).id).OnSelectPop(() -> {
                            mPImgAdapter.get(addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id).openAlbum();
                            mImageAdapterPosition = addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id;
                            mPCurrentImgNum.put(addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id, 0);
                        });
                    } else if ("video".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {
                        //图片控件
                        //外层布局
                        LinearLayout linearLayout = new LinearLayout(application);
                        //加入父布局
                        pLinearLayout.addView(linearLayout);
                        //设置本层布局的背景颜色
                        linearLayout.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA));
                        //设置垂直布局
                        linearLayout.setOrientation(LinearLayout.VERTICAL);

                        //创建一个标题控件标题
                        TextView mTitleTextView = new TextView(application);
                        //把标题控件加入本层布局
                        linearLayout.addView(mTitleTextView);
                        //得到标题控件的 LayoutParams
                        LinearLayout.LayoutParams textViewLayoutParams = (LinearLayout.LayoutParams) mTitleTextView.getLayoutParams();
                        //设置标题边距
                        textViewLayoutParams.bottomMargin = DensityUtils.dip2px(application, 10);
                        textViewLayoutParams.topMargin = DensityUtils.dip2px(application, 10);
                        textViewLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);
                        //设置标题文字
                        mTitleTextView.setText(addLayoutEntityList.get(i).subColumns.get(j).name);
                        //设置标题颜色
                        mTitleTextView.setTextColor(getResources().getColor(R.color.color_333333));
                        //图片recyclerView
                        RecyclerView recyclerView = new RecyclerView(application);
                        //recyclerView加入本层布局
                        linearLayout.addView(recyclerView);
                        //添加底部线条
                        if (j != addLayoutEntityList.get(i).subColumns.size() - 1) {
                            TextView bottomLine = new TextView(application);
                            linearLayout.addView(bottomLine);
                            bottomLine.setBackgroundColor(getResources().getColor(R.color.color_EDEDED));
                            //拿到bottomLine的LayoutParams
                            LinearLayout.LayoutParams bottomLineLayoutParams = (LinearLayout.LayoutParams) bottomLine.getLayoutParams();
                            bottomLineLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;//充满
                            bottomLineLayoutParams.height = 1;
                            bottomLineLayoutParams.rightMargin = DensityUtils.dip2px(application, 5);
                            bottomLineLayoutParams.leftMargin = DensityUtils.dip2px(application, 5);
                        }
                        //拿到recyclerView的LayoutParams
                        LinearLayout.LayoutParams recyclerViewLayoutParams = (LinearLayout.LayoutParams) recyclerView.getLayoutParams();
                        //设置边距
                        recyclerViewLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);
                        recyclerViewLayoutParams.bottomMargin = DensityUtils.dip2px(application, 10);
                        //创建视频选择的adapter
                        VideoAdapter videoAdapter = new VideoAdapter(application, 5);
                        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
                        recyclerView.setLayoutManager(new
                                GridLayoutManager(application, 4));
                        recyclerView.setAdapter(videoAdapter);
                        List<VideoEntity> videoEntities = new ArrayList<>();
                        //数据回显
                        if (!TextUtils.isEmpty(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal) && !"[]".equals(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal)) {
                            List<VideoChangeEntity> videoChangeEntities = new Gson().fromJson(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal, new TypeToken<List<VideoChangeEntity>>() {
                            }.getType());
                            if (videoChangeEntities != null) {
                                for (int x = 0; x < videoChangeEntities.size(); x++) {
                                    VideoEntity videoEntity = new VideoEntity();
                                    //视频类型
                                    videoEntity.type = videoChangeEntities.get(x).type;
                                    //视频id
                                    videoEntity.videoId = videoChangeEntities.get(x).videoId;
                                    //视频名称
                                    videoEntity.title = videoChangeEntities.get(x).videoName;
                                    //视频地址
                                    videoEntity.url = videoChangeEntities.get(x).playUrl;
                                    //视频时长
                                    videoEntity.time = videoChangeEntities.get(x).time;
                                    videoEntities.add(videoEntity);
                                }
                            }
                            videoAdapter.addData(videoEntities);
                        }
                        mPVideoAdapter.put(addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id, videoAdapter);
                        mPVideoIndex.put(addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id, 0);
                        mPVideoFile.put(addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id, new ArrayList<>());
                        mPVideoAdapter.get(addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id).OnSelectPop(new VideoAdapter.ISelectPop() {
                            @Override
                            public void openPop(int index) {
                                mVideoAdapterPosition = addLayoutEntityList.get(finalI1).subColumns.get(childPosition).id;
                                if (mPVideoAdapter.get(mVideoAdapterPosition).getData().size() != 5) {
//                                mPVideoAdapter.get(mVideoAdapterPosition).openVideo(VIDEO_ADAPTER_LIST);
                                    mPVideoIndex.put(mVideoAdapterPosition, index);
                                    //重置弹窗数据
                                    showVideoListSelectPopLD.setValue(VIDEO_ADAPTER_LIST);
                                } else {
                                    Toast.makeText(application, "最多选择五个视频", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void openVideo(VideoEntity url) {

                            }
                        });
                    } else if ("richtext".equals(addLayoutEntityList.get(i).subColumns.get(j).type)) {//富文本
                        //手机不支持编辑，请使用电脑后台编辑
                        if (addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal == null) {
                            //外层布局
                            LinearLayout linearLayout = new LinearLayout(application);
                            //加入父布局
                            pLinearLayout.addView(linearLayout);
                            //设置本层布局的背景颜色
                            linearLayout.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA));
                            //设置垂直布局
                            linearLayout.setOrientation(LinearLayout.VERTICAL);
                            //标题
                            TextView tvTitle = new TextView(application);
                            //加入当前父布局
                            linearLayout.addView(tvTitle);
                            tvTitle.setText(addLayoutEntityList.get(i).subColumns.get(j).name);
                            tvTitle.setTextColor(ColorUtils.getColor(R.color.color_333333));
                            //得到标题控件的 LayoutParams
                            LinearLayout.LayoutParams textViewLayoutParams = (LinearLayout.LayoutParams) tvTitle.getLayoutParams();
                            //设置标题边距
                            textViewLayoutParams.topMargin = DensityUtils.dip2px(application, 10);
                            textViewLayoutParams.bottomMargin = DensityUtils.dip2px(application, 4);
                            textViewLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);

                            //提示文字
                            TextView tvSubTitle = new TextView(application);
                            //加入当前父布局
                            linearLayout.addView(tvSubTitle);
                            tvSubTitle.setText("(暂无数据,手机不支持编辑，请使用电脑后台编辑)");
                            tvSubTitle.setTextColor(ColorUtils.getColor(R.color.color_C0C4CC));
                            tvSubTitle.setTextSize(12f);
                            //得到标题控件的 LayoutParams
                            LinearLayout.LayoutParams tvSubTitleLayoutParams = (LinearLayout.LayoutParams) tvSubTitle.getLayoutParams();
                            //设置标题边距
                            tvSubTitleLayoutParams.bottomMargin = DensityUtils.dip2px(application, 10);
                            tvSubTitleLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);
                        } else {
                            //外层布局
                            LinearLayout linearLayout = new LinearLayout(application);
                            //加入父布局
                            pLinearLayout.addView(linearLayout);
                            //设置本层布局的背景颜色
                            linearLayout.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA));
                            //设置垂直布局
                            linearLayout.setOrientation(LinearLayout.VERTICAL);
                            //标题
                            TextView tvTitle = new TextView(application);
                            //加入当前父布局
                            linearLayout.addView(tvTitle);
                            tvTitle.setText(addLayoutEntityList.get(i).subColumns.get(j).name);
                            tvTitle.setTextColor(ColorUtils.getColor(R.color.color_333333));
                            //得到标题控件的 LayoutParams
                            LinearLayout.LayoutParams textViewLayoutParams = (LinearLayout.LayoutParams) tvTitle.getLayoutParams();
                            //设置标题边距
                            textViewLayoutParams.topMargin = DensityUtils.dip2px(application, 10);
                            textViewLayoutParams.bottomMargin = DensityUtils.dip2px(application, 4);
                            textViewLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);

                            //提示文字
                            TextView tvSubTitle = new TextView(application);
                            //加入当前父布局
                            linearLayout.addView(tvSubTitle);
                            tvSubTitle.setText("(手机不支持编辑，请使用电脑后台编辑)");
                            tvSubTitle.setTextColor(ColorUtils.getColor(R.color.color_C0C4CC));
                            tvSubTitle.setTextSize(12f);
                            //得到标题控件的 LayoutParams
                            LinearLayout.LayoutParams tvSubTitleLayoutParams = (LinearLayout.LayoutParams) tvSubTitle.getLayoutParams();
                            //设置标题边距
                            tvSubTitleLayoutParams.bottomMargin = DensityUtils.dip2px(application, 10);
                            tvSubTitleLayoutParams.leftMargin = DensityUtils.dip2px(application, 10);

                            //WebView布局
                            WebView richTextWebView = new WebView(application);
                            //加入父布局
                            linearLayout.addView(richTextWebView);
                            //得到标题控件的 LayoutParams
                            LinearLayout.LayoutParams webViewLayoutParams = (LinearLayout.LayoutParams) richTextWebView.getLayoutParams();
                            webViewLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
                            richTextWebView.loadDataWithBaseURL(null, setWebViewContent(addLayoutEntityList.get(i).subColumns.get(j).titleColumnVal), "text/html", "UTF-8", null);
                            richTextWebView.getSettings().setJavaScriptEnabled(true);
                            richTextWebView.getSettings().setSupportZoom(false);
                            richTextWebView.getSettings().setBuiltInZoomControls(false);
                            richTextWebView.getSettings().setUseWideViewPort(true);
                            richTextWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
                            richTextWebView.getSettings().setLoadWithOverviewMode(true);
                            richTextWebView.setBackgroundColor(getResources().getColor(R.color.goods_color_FFFAFAFA)); // 设置背景色
                            richTextWebView.requestLayout();
                        }

                        //添加底部线条
                        if (j != addLayoutEntityList.get(i).subColumns.size() - 1) {
                            TextView bottomLine = new TextView(application);
                            pLinearLayout.addView(bottomLine);
                            bottomLine.setBackgroundColor(getResources().getColor(R.color.color_EDEDED));
                            //拿到bottomLine的LayoutParams
                            LinearLayout.LayoutParams bottomLineLayoutParams = (LinearLayout.LayoutParams) bottomLine.getLayoutParams();
                            bottomLineLayoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;//充满
                            bottomLineLayoutParams.height = 1;
                            bottomLineLayoutParams.rightMargin = DensityUtils.dip2px(application, 5);
                            bottomLineLayoutParams.leftMargin = DensityUtils.dip2px(application, 5);
                        }
                    }
                }
            }
        } else {
            mDataBinding.tvOther.setVisibility(View.GONE);
        }
    }

    /**
     * 适配image和table标签
     */
    private String setWebViewContent(String content) {
        String head = "<style type=\"text/css\"> img {" +
                "width:100%;" +//限定图片宽度填充屏幕
                "height:auto;" +//限定图片高度自动
                "}" +
                "body {" +
                "margin-right:15px;" +//限定网页中的文字右边距为15px(可根据实际需要进行行管屏幕适配操作)
                "margin-left:15px;" +//限定网页中的文字左边距为15px(可根据实际需要进行行管屏幕适配操作)
                "margin-top:15px;" +//限定网页中的文字上边距为15px(可根据实际需要进行行管屏幕适配操作)
                "font-size:38px;" +//限定网页中文字的大小为40px,请务必根据各种屏幕分辨率进行适配更改
                "word-wrap:break-word;" +//允许自动换行(汉字网页应该不需要这一属性,这个用来强制英文单词换行,类似于word/wps中的西文换行)
                "}" +
                "</style>";
        return "<html><header>" + head + "</header>" + content + "</html>";
    }
}
